﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MCorder.control;
using MCorder.efside;
using MCorder.motor;
using EastForceCondensor.CommCtrl;
using System.Reflection;
//using System.Timers;

namespace MCorder.control
{
    public struct DevDataSet
    {
        public long timestamp;
        public double pos;
        public double cur;
        public double speed;
        public double voltage;
        public double volt4;
        public int num_cur;
        public int num_pos;
    }   

    public class DevControl
    {
        private Motor MotorControl = new Motor();
        private Hall HallControl = new Hall();
        private DataSaver dataSaver = null;

        //private float coff = 0.00184f;
        public float coff = 1.0f;

        public delegate void delPlaneScanCallback();

        public void Init()
        {
            MotorControl.Init();

            HallControl.Init();
        }
        double pos_temp = 0;
        public double num = 0;
        DateTime dt ;

        private bool saveFlag = false;

        double cur_temp=-1;
        double current = 0;
        double position = 0;
        float receive_base = 0;
        bool useBase = false;
        int num_return = 20;
        //System.Timers.Timer DAQTimer = new System.Timers.Timer();
        //use flag
        public bool GetData(ref DevDataSet devData)
        {
            int a_cur = 0, b_pos = 0;
            devData.num_cur = a_cur = HallControl.ad_current_list.Count();
            devData.num_pos = b_pos = MotorControl.distance_list.Count();

            if (a_cur > 0)
            {
                current = HallControl.ad_current_list[0];
                HallControl.ad_current_list.RemoveAt(0);
            }
            if (b_pos > 0)
            {
                position = MotorControl.distance_list[0];
                MotorControl.distance_list.RemoveAt(0);
            }

            devData.speed = MotorControl.GetVelocity();

            HallControl.ad_current_list.Clear();
            MotorControl.distance_list.Clear();

            if(distance_base>0.1&& num_return>0)
            {
                num_return--;
                return false;
            }
            position = position + distance_base;
            if((current > 0.1)&position>0)
            {
                devData.cur = (current - volt_base) / coff;
                devData.pos = position;
                devData.speed = MotorControl.GetVelocity();
                devData.voltage = current;
               
                if (position != pos_temp)
                {
                    if (saveFlag)
                    {
                        dataSaver.SaveData(devData);
                        if (position<0.1)
                        {
                            if ((a_cur - 1) > 0 | (b_pos - 1) > 0)
                            {
                                HallControl.ad_current_list.Clear();
                                MotorControl.distance_list.Clear();
                            }
                        }
                    }
                }
                pos_temp = devData.pos;
                return true;
            }
            //20210111
            else
            {
                return true;
            }
        }

        private double volt_base = 0.0;
        
        private double distance_base = 0.0;
        public void setCoff(float cc)
        {
            coff = cc;
        }

        private double getVbase()
        {
            float[] add = new float[5];

            float v0 = 0;
            // for (int i = 0; i < 50; i++)
            {
                v0 += (float)HallControl.ad_current_list[0];
                HallControl.ad_current_list.RemoveAt(0);

            }
            // v0 = v0 / 50;
            return v0;
        }
        public void StartRoll_Plane(string fileName1,int direction, double speed,double tension,bool followlasttime, bool OpenfromExtern,double ad_base, List<float> position, List<float> cur,string path_extern)
        {
            if (!followlasttime)
            {
                ad_base = getVbase();//获取基底数据
                volt_base = ad_base;
            }

            if (followlasttime)
            {
                if (OpenfromExtern == true)
                {
                    distance_base = Convert.ToDouble(position[position.Count() - 1]);
                    OpenfromExtern = false;
                    if (fileName1 != "")
                    {
                        dataSaver = new DataSaver(fileName1, path_extern, true, fileName1);
                    }
                    else
                    {
                        dataSaver = new DataSaver(DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"), path_extern, true, DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"));
                        volt_base = ad_base;
                    }
                }
                else
                {
                    distance_base = 0;
                    // distance_base = Convert.ToDouble(position[position.Count() - 1]);
                }
            }

            if (followlasttime == false)
            {

                if (fileName1 == "")
                {
                    dataSaver = new DataSaver(DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"), null, false, DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"));
                }
                else
                {
                    dataSaver = new DataSaver(fileName1 + "_" + DateTime.Now.ToString("yyyyMMdd-HH-mm-ss"), path_extern, false, fileName1);
                }
                dataSaver.SaveHeader(coff, ad_base, speed);


            }

            if (followlasttime == false)
            {
                MotorControl.SetZeroEp();
            }
            //begintime = DateTime.Now.Ticks ;

            saveFlag = true;

            tension += 0.1;

            if (direction == 0)//right
            {
                MotorControl.DIR = 1;
                MotorControl.setclutch(tension, 9.8);
                //MotorControl.setclutch(1.0, 4.5);
            }
            else
            {
                MotorControl.DIR = 0;
                MotorControl.setclutch(9.8, tension);
                //MotorControl.setclutch(4.5,1.0);
            }
            MotorControl.speed_Aim = speed;

            MotorControl.start();
            HallControl.start();
            //DAQTimer.Enabled = true;
        }


        public void StopRoll()
        {
            saveFlag = false;
            MotorControl.stop();
            //HallControl.Disconnect();
            //DAQTimer.Enabled = false;

        }
        public bool isRun()
        {
            if (MotorControl.isRun && HallControl.isRun)
            {
                return true;
            }
            else
                return false;
        }

        public void StopDataSaving()
        {
            dataSaver.SaveEnd();
        }
    }
}